package aip.project.service;

import java.util.List;
import java.util.Map;

import org.slim3.controller.upload.FileItem;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import aip.project.meta.OfferteMeta;
import aip.project.model.Offerte;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;


public class OfferteService {

    private OfferteMeta meta = new OfferteMeta();

    public Offerte getOfferta(Key k){
        Offerte o = Datastore.get(Offerte.class, k);        
        return o;
    }
    
    public List<Offerte> getOffertaList(){
        return Datastore.query(meta).sort(meta.nome.asc).asList();
    }
    
    public Key addOfferta (Map<String, Object> nome){
        Offerte o = new Offerte();
        BeanUtil.copy(nome, o);
        FileItem imageFile = (FileItem) nome.get("image");
        if (imageFile != null) {
            o.setImmagine(new Blob(imageFile.getData()));
        }
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(o);
        tx.commit();
        return o.getKey();
    }
    
    
    
    public void removeOfferta (Key k){
        Datastore.delete(k);
    }
}
